package com.ruoyi.web.controller.system;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysMenu;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.*;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.ConnectUserService;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.service.ISysMenuService;
import com.ruoyi.web.controller.service.VimUserService;
import com.sun.corba.se.impl.protocol.RequestDispatcherRegistryImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Base64;
import java.util.List;
import java.util.Set;

//import com.ruoyi.web.controller.service.VimUserService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ConnectUserService connectUserService;

    @Autowired
    private VimUserService vimUserService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
//        String a = connectUserService.loginApi(loginBody);
//        Token token = JSON.parseObject(a,Token.class);
//        String aa = token.getData().getToken();
//        if(a == null && a.equals(" ")){
//            return AjaxResult.error("账号信息异常");
//        }
//        String b = connectUserService.checkToken(aa);
//        UserInfo userInfo = JSON.parseObject(b,UserInfo.class);
//        if(b == null && b.equals(" ")){
//            return AjaxResult.error(userInfo.getMessage());
//        }
//        //通过登录的用户名进行查询
//        SysUser sysUser1 = vimUserService.select(loginBody);
//        if(sysUser1 == null){
//            SysUser sysUser = new SysUser();
//            sysUser.setUserName(userInfo.getData().getUsername());
//            sysUser.setPassword(SecurityUtils.encryptPassword(loginBody.getPassword()));
//            sysUser.setDeptId(103L);
//            sysUser.setNickName(userInfo.getData().getRealName());
//            sysUser.setMobile(userInfo.getData().getUserPhone());
//            sysUser.setStatus("0");
//            sysUser.setDelFlag("0");
//            vimUserService.add(sysUser);
//        }
//        redisTemplate.opsForValue().set("token" + loginBody.getUsername(), token);
            AjaxResult ajax = AjaxResult.success();
    // 生成令牌
        // 解码Base64编码字符串
        byte[] decodedBytes = Base64.getDecoder().decode(loginBody.getPassword());
        // 将字节数组转换为字符串
        String a = new String(decodedBytes);
        loginBody.setPassword(a);
        String tokens = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
            loginBody.getUuid());
//        String a = SecurityUtils.encryptPassword("12345qwert!");
//        System.out.println(a + "............................................");
        ajax.put(Constants.TOKEN, tokens);
        return ajax;
}

    /**
         * 获取用户信息
         *
         * @return 用户信息
         */
        @GetMapping("getInfo")
        public AjaxResult getInfo ()
        {
            SysUser user = SecurityUtils.getLoginUser().getUser();
            // 角色集合
            Set<String> roles = permissionService.getRolePermission(user);
            // 权限集合
            Set<String> permissions = permissionService.getMenuPermission(user);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("user", user);
            ajax.put("roles", roles);
            ajax.put("permissions", permissions);
            return ajax;
        }

        /**
         * 获取路由信息
         *
         * @return 路由信息
         */
        @GetMapping("getRouters")
        public AjaxResult getRouters()
        {
            Long userId = SecurityUtils.getUserId();
            List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
            return AjaxResult.success(menuService.buildMenus(menus));
        }
}